package com.stockz.equity.charting.view.widget;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;

import org.springframework.aop.target.dynamic.Refreshable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.display.indicator.AbstractDisplayableIndicatorConfiguration;
import com.stockz.equity.charting.event.EventPublisher;
import com.stockz.equity.charting.event.RepaintStockChartPanelEvent;
import com.stockz.equity.charting.util.ContextUtil;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.charting.view.toolbar.IndicatorToolBar;
import com.stockz.equity.charting.view.widget.custom.FilteredComboBox;
import com.stockz.equity.charting.view.widget.custom.FilteredComboBox.OnSelectionCallback;
import com.stockz.equity.model.Bar.BarValueType;
import com.stockz.equity.pattern.Child;

@Component
public class IndicatorComboBox extends FilteredComboBox implements
		OnSelectionCallback,Child,Refreshable,Ordered {
	private static final long serialVersionUID = 8105276695694111976L;
	
	@Autowired private SpanTextField spanTextField;
	@Autowired private BackedByComboBox backedByComboBox;
	@Autowired StockChartView stockChartView;
	
	@SuppressWarnings({ "rawtypes" })
	private List<Class> parentList = new ArrayList<Class>(1);
	
	public IndicatorComboBox(){
		parentList.add(IndicatorToolBar.class);
		setOnSelectionCallback(this);
	}
	
	@Override
	public void onSelection(Object selectedItem){
		AbstractDisplayableIndicatorConfiguration indicatorDisplay = (AbstractDisplayableIndicatorConfiguration)getSelectedItem();
		
		if(backedByComboBox.getSelectedIndex() != -1){
			indicatorDisplay.setBarValueType((BarValueType)backedByComboBox.getSelectedItem());
		}else{
			indicatorDisplay.setBarValueType(BarValueType.CLOSE);
		}
		indicatorDisplay.setStockChartView(stockChartView);
		indicatorDisplay.refreshValues();
		
		stockChartView.getSelectedStockChart().getStockChartPanel()
		.getStockChartPanelState().getDisplayableSet().add(indicatorDisplay);
		
		EventPublisher.publishEvent(
				new RepaintStockChartPanelEvent(
						stockChartView.getSelectedStockChart().getStockChartPanel()));
	}

	@SuppressWarnings("unchecked")
	@Override
	public void refresh() {
		Map<String,AbstractDisplayableIndicatorConfiguration> indicatorDisplayMap 
		= ContextUtil.getApplicationContext().getBeansOfType(AbstractDisplayableIndicatorConfiguration.class);
		setModel(new DefaultComboBoxModel(indicatorDisplayMap.values().toArray()));
	}

	@SuppressWarnings({ "rawtypes" })
	@Override
	public Collection<Class> getParents() {
		return parentList;
	}

	@Override
	public long getLastRefreshTime() {
		return 0;
	}

	@Override
	public long getRefreshCount() {
		return 0;
	}

	@Override
	public int getOrder() {
		return 0;
	}
}
